package 原子类;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicMarkableReference;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2022-08-29 10:40
 **/
public class AtomicMarkableReferenceDemo {

    private static final AtomicMarkableReference<Integer> MARKABLEREFERENCE = new AtomicMarkableReference<>(100, false);

    public static void main(String[] args) {
        new Thread(() -> {
            final boolean marked = MARKABLEREFERENCE.isMarked();
            System.out.println(Thread.currentThread().getName() + "\t" + "默认标识：" + marked);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            MARKABLEREFERENCE.compareAndSet(100, 1000, marked, !marked);
        }, "T1").start();

        new Thread(() -> {
            final boolean marked = MARKABLEREFERENCE.isMarked();
            System.out.println(Thread.currentThread().getName() + "\t" + "默认标识：" + marked);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            final boolean b = MARKABLEREFERENCE.compareAndSet(100, 2000, marked, !marked);
            System.out.println(Thread.currentThread().getName() + "\t" + "T2线程CAS结果：" + marked);
            System.out.println(Thread.currentThread().getName() + "\t" + MARKABLEREFERENCE.isMarked());
            System.out.println(Thread.currentThread().getName() + "\t" + MARKABLEREFERENCE.getReference());
        }, "T2").start();

    }
}
